﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Logistics;
using Logistics.DataManage.Server;

namespace DataUpload
{
    /// <summary>
    /// 注册申请验证类
    /// </summary>
    public class RegistrationBase
    {
        /// <summary>
        /// 获取当前机器的注册信息
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns>
        /// -2:系统文件损坏
        /// -1:未注册
        /// 0:已注册，未审核
        /// 1:已审核
        /// </returns>
        public static int GetRegeditedState(string xmlPath)
        {
            string rootPath = "/system/SecurityCheck/";
            string regPath = rootPath + "ApplicationRegedit/IsApplication";
            string chePath = rootPath + "ApplicationChecked/IsChecked";

            string regRtn = "";
            string cheRtn = "";
            try
            {
                regRtn = GetXmlDocumentNodeValue(xmlPath, regPath, "");//注册信息
                cheRtn = GetXmlDocumentNodeValue(xmlPath, chePath, "");//审核信息
            }
            catch
            {
                return -2;//软件配置文件损坏
            }
            if(String.IsNullOrEmpty(regRtn) || String.IsNullOrEmpty(cheRtn))
                return -2;//软件配置文件损坏
            if (String.Equals(regRtn, "false"))//未申请注册
            {
                return -1;
            }
            else//已注册
            {
                /*判断是否已经通过审核*/
                if (String.Equals(cheRtn, "true"))//未审核
                    return 1;
                else//已审核
                    return 0;
            }
        }
        /// <summary>
        /// 由XML文件路径得到XML文档对象
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static XmlDocument GetXmlDocument(string filePath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            if (File.Exists(filePath))
            {
                xmlDoc.Load(filePath);
            }
            return xmlDoc;
        }
        /// <summary>
        /// 获取XML文件节点值
        /// </summary>
        /// <param name="filePath">XML文件路径</param>
        /// <param name="nodePath">
        /// 节点路径,形式如下：
        ///     /system/SecurityCheck/ApplicationRegedit/IsApplication
        /// </param>
        /// <param name="nodeAttr"></param>
        /// <returns></returns>
        public static string GetXmlDocumentNodeValue(string filePath, string nodePath, string nodeAttr)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc = GetXmlDocument(filePath);
            return GetXmlDocumentNode(xmlDoc, nodePath, nodeAttr);
        }
        /// <summary>
        /// 获取XML文件的某个节点值
        /// </summary>
        /// <param name="xmlDoc">XML文档</param>
        /// <param name="nodePath">
        /// 节点路径,形式如下：
        ///     /system/SecurityCheck/ApplicationRegedit/IsApplication
        /// </param>
        /// <param name="nodeAttr">
        /// 节点属性,如果为空，则取节点文本值
        /// </param>
        /// <returns></returns>
        public static string GetXmlDocumentNode(XmlDocument xmlDoc, string nodePath, string nodeAttr)
        {
            string rtnStr = "";
            if (xmlDoc == null)
                return rtnStr;
            XmlNodeList nodes = xmlDoc.SelectSingleNode(nodePath).ChildNodes;
            if (nodes == null || nodes.Count == 0)
                return "";
            XmlNode node = nodes[0];
            if (String.IsNullOrEmpty(nodeAttr))
                rtnStr = node.Value;
            else
                rtnStr = node.Attributes[nodeAttr].Value;
            return rtnStr;
        }

        /// <summary>
        /// 修改XMl文件节点属性值
        /// </summary>
        /// <param name="filePath">xml文件路径</param>
        /// <param name="nodePath">
        /// 节点路径,形式如下：
        ///     /system/SecurityCheck/ApplicationRegedit/IsApplication
        /// </param>
        /// <param name="nodeAttr">
        /// 节点属性,如果为空，则取节点文本值
        /// </param>
        /// <param name="value">修改后的值</param>
        /// <returns>成功:true;失败:false</returns>
        public static bool ModifyXmlNode(string filePath,string nodePath ,string nodeAttr,string value)
        {
            bool rtnState = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlNodeList nodes = xmlDoc.SelectSingleNode(nodePath).ChildNodes;
            if (nodes == null || nodes.Count == 0)
                rtnState= false;
            XmlNode node = nodes[0];
            if (String.IsNullOrEmpty(nodeAttr))
            {
                node.InnerText=value;
            }
            else
            {
                node.Attributes[nodeAttr].Value = value;
            }
            try
            {
                xmlDoc.Save(filePath);
                rtnState= true;
            }
            catch {
                rtnState = false;
            }
            return rtnState;
        }

        /// <summary>
        /// 申请注册
        /// </summary>
        /// <param name="createUser"></param>
        /// <param name="psd"></param>
        /// <param name="remark"></param>
        public static string ApplicationRegedit(string createUser, string psd,string remark)
        {
            int userState = GetUserState(createUser, psd);
            if (userState == -1)//用户不存在
                return "-2";
            else if (userState == 0)//用户不可用
                return "-1";
            else{
                string mac = MachineCode.GetMachineConfig("mac");
                string hdId = MachineCode.GetHdId();
                string hostName = MachineCode.GetMachineConfig("host");
                string cpu = MachineCode.GetMachineCPU();
                string ip = MachineCode.GetMachineConfig("ip");
                List<string> lst = new List<string>();
                lst.Add(mac);
                lst.Add(hdId);
                lst.Add(cpu);
                lst.Add(ip);
                lst.Add(hostName);
                lst.Add(createUser);
                lst.Add(remark);
                try
                {
                    return RemoteDateServiceFactory.DateService.ApplicationRegedit(lst).ToString(); 
                    //return (string)result;
                }
                catch (Exception ex){
                    return ex.ToString();
                }
            }
        }

        private static int GetUserState(string userName, string psd)
        {
            object[] args = { userName,psd };
            try
            {
                object result = RemoteDateServiceFactory.DateService.GetUserState(userName, psd);
                return (int)result;
            }
            catch {
                return 0;
            }
        }
    }
}
